#!/bin/bash

# Pound on syscall interface

# Copyright (C) 2003-2006 IBM
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

RANDASYS_USER=daemon
RANDASYS_UID=`id -u $RANDASYS_USER`
RANDASYS_GROUP=daemon
RANDASYS_GID=`id -u $RANDASYS_GROUP`

# Try to find randasyscall on the system
PROGRAM=`ls "$POUNDER_TMPDIR/randasys" 2> /dev/null`
if [ -z "$PROGRAM" ]; then
	PROGRAM=`ls "$POUNDER_SRCDIR/randasyscall/randasys"`
	if [ -z "$PROGRAM" ]; then
		echo "Cannot find randasys; did you run Install?"
		exit -1
	fi
fi

trap 'rm -rf "$POUNDER_TMPDIR/randasys"; kill -9 `pgrep -P $$` 2> /dev/null; exit 0' 1 2 15

# Change the permissions on the log file
chown $RANDASYS_USER `ls -la /proc/$$/fd/1 | awk -F " " '{print $11}'`

# We need to dump this somewhere where the daemon user can read this
cp "$PROGRAM" "/tmp"
PROGRAM="/tmp/randasys"

# Save a copy of dmesg
dmesg > "$POUNDER_TMPDIR/randasys-before"

# Start the program
for ((k=0; k < $NR_CPUS; k++))
do
	"$POUNDER_HOME/fancy_timed_loop" 900 $RANDASYS_UID $RANDASYS_GID 9 "$PROGRAM" &
	"$POUNDER_HOME/fancy_timed_loop" 900 $RANDASYS_UID $RANDASYS_GID 9 "$PROGRAM" -z &
done

# Wait for this to finish (it won't)
wait

# Save another copy of dmesg
dmesg > "$POUNDER_TMPDIR/randasys-after"

# Diff
DMESG_DIFF_LINES=`diff "$POUNDER_TMPDIR/randasys-before" "$POUNDER_TMPDIR/randasys-after" | wc -l`
diff "$POUNDER_TMPDIR/randasys-before" "$POUNDER_TMPDIR/randasys-after"
rm -rf "$POUNDER_TMPDIR/randasys-before" "$POUNDER_TMPDIR/randasys-after"

# Failures will show up in the test output.  Or better yet,
# panic/oops/BUG the machine.
exit 0
